<?php
/*
 module:		红娘管理->付费项目设置
 create_time:	2024-03-25 10:01:45
 author:	937658938@qq.com
 contact:
*/
namespace app\api\controller;

use app\api\controller\Common;
use app\backend\service\ZhMatchmakerChargeService;

class ZhMatchmakerCharge extends Common{

    /**
     * @return \think\response\Json
     *  @apiParam (输入参数：) {string}            limit 分页查询每一页显示多少条，默认20条(可选)
     *  @apiParam (输入参数：) {int}               offset 分页查询当前第几页，默认0 (可选)
     *  @apiParam (输入参数：) {string}            entry_name 收费项目名称 (可选)
     *  @apiParam (输入参数：) {string}            is_fixed_price 收费模式（0：固定金额，1：红娘自定义） (可选)
     *
     * @apiSuccessExample {json} 01 成功示例
     *  [{"id":9,"entry_name":null,"matchmaker_money_json":[{"level_id":"10","commission_rate":"10%"},{"level_id":"8","commission_rate":"20%"},{"level_id":"9","commission_rate":"30%"}],"comment":null,"is_fixed_price":0,"createtime":"2024-03-26 18:53:52"},{"id":8,"entry_name":null,"matchmaker_money_json":[{"level_id":"8","toll_money":"199","commission_rate":"100"},{"level_id":"8","toll_money":"299","commission_rate":"200"},{"level_id":"9","toll_money":"399","commission_rate":"300"}],"comment":null,"is_fixed_price":1,"createtime":"2024-03-26 18:53:17"}]
     * matchmaker_money_json参数说明：
     *      level_id：红娘等级id，
     *      toll_money：收费金额(固定金额模式才有这个字段)，
     *      commission_rate：佣金（固定金额模式单位：元，例如：500，红娘自定义模式：单位是百分比,例如10%））
     *
     * @apiErrorExample {json} 02 失败示例
     *  {"status":" 201","msg":"操作失败"}
     */
    function index()
    {
//        $wxapp_id = $this->request->get('wxapp_id');
//        if (!$wxapp_id) {
//            return $this->ajaxReturn($this->errorCode, '缺少平台参数');
//        }

        //查询所有的收费项目
        $limit           = $this->request->post('limit', 20, 'intval');
        $offset          = $this->request->post('offset', 0, 'intval');
        $where['entry_name']   = $this->request->param('entry_name', '', 'serach_in');
        $where['is_fixed_price'] = $this->request->param('is_fixed_price', '', 'serach_in');

        $page = floor($offset / $limit) + 1;

        $res  = ZhMatchmakerChargeService::indexList(formatWhere($where), $limit, $page);

        $resJson = [];
        foreach ($res as $key => $value){
            foreach ($value as $k1=>$v1) {
                //解码json
                $row = ZhMatchmakerChargeService::convertToJosn($v1);
                array_push($resJson,$row);
            }
        }
        return $this->ajaxReturn($this->successCode, '返回成功', $resJson);
    }

    /**
     * 某个收费项目的详情
     * @return void
     */
    function detail(){
        $id      =  $this->request->get('id');
        $m_id=   $this->request->get('m_id');
        $res  = \app\backend\model\ZhMatchmakerCharge::find($id);
        $res["matchmaker_money_json"]=json_decode(html_entity_decode($res["matchmaker_money_json"]),1);
        if(intval($m_id)>0){
            $matchmaker=\app\api\model\ZhMatchmaker::find($m_id);
            if(!$matchmaker){
                return $this->ajaxReturn($this->errorCode, '未知红娘用户');
            }else{
                if($res["is_fixed_price"]==0){
                    $level=$matchmaker["level_id"];
                    if($level<=0){
                        return $this->ajaxReturn($this->errorCode, "红娘用户级别未设置，项目id{$id} 红娘id {$m_id}");
                    }
                    foreach ($res["matchmaker_money_json"] as $k=>$v){
                        if($v["level_id"]==$level){
                            if(intval($res["is_fixed_price"]==0)){
                                $res["pay_price"]=$v["toll_money"];
                            }else{
                                $res["pay_price"]=0;
                            }
                        }

                    }
                    $res["pay_price_desc"]="红娘级别{$level}对应金额";
                }else{
                    $res["pay_price"]=0;
                    $res["pay_price_desc"]="红娘自定义金额";
                }

            }
        }
        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }


}

?>